import React from 'react';
import { observable, action, toJS } from 'mobx';
import * as Apis from "../../api/customerApis";
import { Tools } from 'weaver-mobile';
import { SearchAdvanced, RouteLayout,ListViewPage } from 'weaver-mobile-page';
import ContactList from '../../components/list/ContactList';
const getLabel = Tools.getLabel;
export default class SellsReprtStore {

    @observable comObj = {};
    @observable search = {};
    @observable createdateselectParams = { createdateselect: "2" };//默认条件本周
    @observable createrselectParams = { creatertype: "4" };//默认条件本部门
    @observable tabSelecteParams = { reporttype: "addCustomersCount" };
    @observable createDateSelectOptions = [];//创建日期下拉选项
    @observable createrSelectOptions = [];//创建人员下拉选项
    @observable sellChanceDateStr = "";//销售简报创建日期文本描述（store里用）
    @observable sellChancePersonStr = "";//销售简报人员范围文本描述（store里用）
    @observable sellChanceSearchStr = "";//销售简报筛选文本描述（compnent里用）
    @observable list = {};
    @observable detailLayout = {};
    @observable chartOption = {};
    @observable rowId = 0;
    @observable contactListParams = {};
    @observable ContactListComp = [];



    @action
    doNew = (comObj) => {
        const route = comObj.props.match.path;
        this.comObj = comObj;
        this.contactListParams = {};
        this.search = new SearchAdvanced.Store({ route: route, id: 'search' });
        this.list = new ListViewPage.Store({ route: route, id: 'list' });
        if (comObj.props.params) {
            const createdateselect = comObj.props.params.createdateselect;
            this.createdateselectParams = { createdateselect: createdateselect };
            if (createdateselect === '6') {
                this.createdateselectParams.createdatefrom = comObj.props.params.createdatefrom;
                this.createdateselectParams.createdateto = comObj.props.params.createdateto;
            }
            const creatertype = comObj.props.params.creatertype;
            const createrids = comObj.props.params['createrid' + creatertype];
            this.createrselectParams = { creatertype: creatertype };
            if (creatertype === '1' || creatertype === '2' || creatertype === '3') {
                this.createrselectParams['createrid' + creatertype] = createrids;
                this.createrselectParams.createrids = comObj.props.params.createrids;
            }
            this.tabSelecteParams = { reporttype: comObj.props.params.reporttype };
        }
        
    }

    @action
    doInit = () => {
        this.getHpSellsCondition();//获取销售简报高级搜索条件
    }

    //获取销售简报的高级搜索表单
    @action
    getHpSellsCondition = () => {
        const sellChanceSearchValue = toJS({ ...this.createdateselectParams, ...this.createrselectParams});
        delete sellChanceSearchValue['createrid' + sellChanceSearchValue.creatertype];
        this.search.getData({
            url: '/api/crm/mobile/getHpSellsCondition',
            method: 'POST',
            params: toJS(sellChanceSearchValue)
        }).then((data) => {
            this.createDateSelectOptions = data.conditioninfo[0].items[0].options;
            this.createrSelectOptions = data.conditioninfo[0].items[1].options;

            if (this.comObj.props.formData && this.search.form) {
                this.search.form.formData = this.comObj.props.formData;

                if (this.createdateselectParams.createdateselect === '6') {
                    this.search.form.formData.createdateselect__createdatefrom__createdateto = this.comObj.props.formData.createdateselect__createdatefrom__createdateto;
                }
                if (this.createrselectParams.creatertype === '1' || this.createrselectParams.creatertype === '2' || this.createrselectParams.creatertype === '3') {
                    this.search.form.formData.creatertype__createrid1__createrid2__createrid3 = this.comObj.props.formData.creatertype__createrid1__createrid2__createrid3;
                }
            }
            this.getHpSellsInfo();//获取销售简报信息
        });
    }

    //获取销售简报统计信息
    @action
    getHpSellsInfo = () => {
       
        const sellChanceSearchValue = toJS({ ...this.createdateselectParams, ...this.createrselectParams, ...this.tabSelecteParams });
        // this.getSellChanceSearchValue(sellChanceSearchValue);
        this.getSellChanceSearchStr();
        delete sellChanceSearchValue['createrid' + sellChanceSearchValue.creatertype];
        this.getHpSellsList();

        Apis.getHpSellsReportInfo(toJS(sellChanceSearchValue)).then((result) => {
            this.chartOption = {
                title: {
                    text: getLabel(355,'总数') + `：${result.count}`,
                    // subtext:this.reportTitle
                },
                tooltip: { trigger: 'axis' },
                legend: {
                    data: [getLabel(1331,'数量')],
                },
                toolbox: {
                    show: false,
                    feature: {
                        mark: { show: true },
                        dataView: { show: true, readOnly: false },
                        magicType: { show: true, type: ['line', 'bar'] },
                        restore: { show: true },
                        saveAsImage: { show: true }
                    }
                },
                xAxis: {
                    data: toJS(result.xAxis),
                    // type : 'category',
                    boundaryGap : false,
                    // axisLabel :{rotate:0}
                },
                yAxis: {minInterval:1},
                series: [{
                    // smooth:true,
                    // itemStyle: {normal: {areaStyle: {type: 'default'}}},
                    name: getLabel(1331,'数量'),
                    type: 'line',
                    data: toJS(result.series),
                }],
            }
        });
    }

    //获取搜索表单数据（表单数据变更时触发）
    @action
    getSellChanceSearchValue = (params) => {
        if (params.createdateselect) {
            if (params.createdateselect === '6') {
                if (!params.createdatefrom && !params.createdateto) {
                    return;
                }
            }
            this.createdateselectParams = params;
        } else if (params.creatertype) {
            if (params.creatertype === '1' || params.creatertype === '2' || params.creatertype === '3') {
                let createrids = [];
                if (params['createrid' + params.creatertype] && params['createrid' + params.creatertype].length > 0) {
                    params['createrid' + params.creatertype].forEach((_el) => {
                        createrids.push(_el.id);
                    });
                    params.createrids = `${createrids}`;
                } else {
                    return;
                }
            }
            this.createrselectParams = params;
        }

    }

    //获取创建日期的文本描述
    getSellChanceSearchStr = () => {
        const sellChanceSearchValue = toJS({ ...this.createdateselectParams, ...this.createrselectParams });

        const createdateselect = sellChanceSearchValue.createdateselect;
        this.createDateSelectOptions.forEach((option) => {
            if (createdateselect === option.key) {
                this.sellChanceDateStr = option.showname;
            }
        });

        let sellChancePerson = [];
        const creatertype = sellChanceSearchValue.creatertype;
        if (creatertype === '1' || creatertype === '2' || creatertype === '3') {
            const createOption = sellChanceSearchValue['createrid' + creatertype];
            if (createOption) {
                createOption.forEach((option) => {
                    sellChancePerson.push(option.name);
                });
            }
            if (sellChancePerson && sellChancePerson.length > 0) {
                this.sellChancePersonStr = ' | ' + `${sellChancePerson}`;
            }
        } else {
            this.createrSelectOptions.forEach((option) => {
                if (creatertype === option.key) {
                    this.sellChancePersonStr = ' | ' + option.showname;
                }
            });
        }

        this.sellChanceSearchStr = this.sellChanceDateStr + this.sellChancePersonStr;
    }

    //销售简报搜索条件重置方法
    @action
    getHpSellsReset = () => {
        this.createdateselectParams = { createdateselect: "2" };//默认条件本周
        this.createrselectParams = { creatertype: "4" };//默认条件本部门
    }

    @action
    doClear = () => {
        this.comObj={};
    }


    @action
    onTabChange = (params) => {
        if (params.key) {
            this.tabSelecteParams = { reporttype: params.key };
            this.getHpSellsInfo();
        }

    }

    @action
    getHpSellsList = () => {
        const sellChanceSearchValue = toJS({ ...this.createdateselectParams, ...this.createrselectParams, ...this.tabSelecteParams});
        delete sellChanceSearchValue['createrid' + sellChanceSearchValue.creatertype];
        if(this.tabSelecteParams.reporttype==='addSellsCount'||this.tabSelecteParams.reporttype==='addCustomersCount'){
            Apis.getHpSellsReportList(sellChanceSearchValue).then(data => { 
                const dataKey = data.datas;
                this.list.clearData();
                this.list.getData({ dataKey });
              });

        }else{
            this.ContactListComp=<ContactList params={sellChanceSearchValue} />;
        }
    }

    @action
    setRowId = (rowId) => {
        this.rowId = rowId;
        const route = this.comObj.props.match.path;
        this.detailLayout = new RouteLayout.Store({ route: route, id: rowId, init: true });
        this.comObj.props.history.push(this.detailLayout.path);
    }
}